package remoteService;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ClientHelper {
	String host = "localhost";
	int portNum = 8000;

	public ClientHelper() {
	}

	/**
	 * public static void main(String args[]){ ClientHelper ch = new
	 * ClientHelper(); String reply =
	 * ch.sendToNet("Login 1 001 teacherAccounts.txt");
	 * System.out.println("InClassClientHelperclient:The reply is " + reply); }
	 */
	public String sendToNet(String send) {
		String reply = "";
		Socket socket = null;
		PrintWriter pw = null;
		BufferedReader br = null;
		try {
			socket = new Socket(host, portNum);

			pw = new PrintWriter(socket.getOutputStream());
			br = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));

			pw.write(send);
			pw.flush();
			socket.shutdownOutput();
			String temp;
			while ((temp = br.readLine()) != null) {
				reply += temp;
			}
			System.out.println("InClassClientHelper:client get reply" + reply);
		} catch (IOException e) {
			// TODO: handle exception
			System.out.println("errorInSendToNet" + e);
		} finally {
			try {
				pw.close();
				br.close();
				socket.close();
			} catch (IOException e2) {
				// TODO: handle exception
				System.out.println("Error in close socket" + e2);
			}
		}

		return reply;
	}
}
